package com.szt.sy.service.blog.articletag;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.szt.sy.entity.blog.ArticleTag;
import com.szt.sy.mapper.blog.ArticleTagMapper;

/***
 * 文章标签服务
 * 
 * @author wwy
 *
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class ArticleTagService extends ServiceImpl<ArticleTagMapper, ArticleTag> {
	
	public ArticleTag addRecord(String name) {
		ArticleTag articleTag = new ArticleTag();
		articleTag.setName(name);
		articleTag.setNum(0);
		insert(articleTag);
		return articleTag;
	}
	
	public ArticleTag findOneByName(String name) {
		return selectOne(new EntityWrapper<ArticleTag>().eq("name", name));
	}
	
	/***
	 * 增加数量，数量不重要，采用这个方式就行
	 *
	 * @param name
	 */
	public void increase(String name) {
		ArticleTag articleTag = findOneByName(name);
		if (articleTag == null) {
			articleTag = addRecord(name);
		}
		articleTag.setNum(articleTag.getNum() + 1);
		updateById(articleTag);
	}
	
	public void decrease(String name) {
		ArticleTag articleTag = findOneByName(name);
		if (articleTag != null) {
			articleTag.setNum(articleTag.getNum() - 1);
			updateById(articleTag);
		}
	}
	
	/**
	 * 列表
	 *
	 * @param page
	 * @
	 */
	public Page<ArticleTag> list(Page<ArticleTag> page, Map<String, Object> map) {
		page.setRecords(baseMapper.datalistPage(page, map));
		return page;
	}
	
	/**
	 * 列表(全部)
	 *
	 * @param pd
	 * @
	 */
	public List<ArticleTag> listAll(Map<String, Object> map) {
		return baseMapper.listAll(map);
	}
	
}
